import router from "@/router";
import NProgress from "nprogress"; // progress bar
import "nprogress/nprogress.css"; // progress bar style
import store from "@/store";
import getPageTitle from "@/utils/get-page-title";
//白名单
const whiteList = ["/login", "/404"];
router.beforeEach(async (to, from, next) => {
  NProgress.start(); //开启进度条
  const token = store.state.user.token;
  document.title = getPageTitle(to.meta.title);
  if (token) {
    //有登录
    if (to.path === "/login") {
      console.log("已登录，跳转首页");
      next("/");
      NProgress.done(); //指定跳转不会自动关闭进度条，需要手动添加
    } else {
      if (!store.getters.userId) {
        // 用户基本信息
        await store.dispatch("user/getUserInfo");
      }
      next();
    }
  } else {
    //未登录
    if (whiteList.includes(to.path)) {
      console.log("已有白名单，登录");
      next();
    } else {
      next("/login");
      NProgress.done(); //指定跳转不会自动关闭进度条，需要手动添加
    }
  }
});

router.afterEach(() => {
  NProgress.done();
});
